package cn.virens.web.components.shiro.comm;

import java.util.Collection;

import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;

import cn.hutool.core.collection.CollUtil;
import cn.virens.database.mysql.model.sys.SysUser;

public class SessionUtil {
	public final static String LOGIN_UUID = "SESSION_UUID";
	public final static String LOGIN_NAME = "SESSION_NAME";
	public final static String LOGIN_AVATAR = "SESSION_AVATAR";

	public static final String LOGIN_ROLE_KEY = "SESSION_ROLE_KEY";
	public static final String LOGIN_GROUP_KEY = "SESSION_GROUP_KEY";

	public final static String LOGIN_USER_INFO = "SESSION_USER_INFO";

	/** 获取 当前登录的用户 */
	public static SysUser getUserInfo(Session session) {
		return (SysUser) session.getAttribute(LOGIN_USER_INFO);
	}

	/** 获取 当前登录的用户 */
	public static SysUser getUserInfo(HttpSession session) {
		return (SysUser) session.getAttribute(LOGIN_USER_INFO);
	}

	/** 获取登录用户的唯一识别码 */
	public static String getUserUuid(Session session) {
		return (String) session.getAttribute(LOGIN_UUID);
	}

	/** 获取登录用户的唯一识别码 */
	public static String getUserUuid(HttpSession session) {
		return (String) session.getAttribute(LOGIN_UUID);
	}

	/** 获取登录用户的用户名称 */
	public static String getUserNume(Session session) {
		return (String) session.getAttribute(LOGIN_NAME);
	}

	/** 获取登录用户的用户头像 */
	public static String getUserAvatar(HttpSession session) {
		return (String) session.getAttribute(LOGIN_AVATAR);
	}

	/** 获取登录用户的用户头像 */
	public static String getUserAvatar(Session session) {
		return (String) session.getAttribute(LOGIN_AVATAR);
	}

	/** 获取登录用户的用户名称 */
	public static String getUserNume(HttpSession session) {
		return (String) session.getAttribute(LOGIN_NAME);
	}

	/** 获取登录用户的角色标识 */
	public static String getRoleKeys(Session session) {
		return (String) session.getAttribute(LOGIN_ROLE_KEY);
	}

	/** 获取登录用户的角色标识 */
	public static String getRoleKeys(HttpSession session) {
		return (String) session.getAttribute(LOGIN_ROLE_KEY);
	}

	/** 获取登录用户的角色ID */
	public static String getGroupKeys(Session session) {
		return (String) session.getAttribute(LOGIN_GROUP_KEY);
	}

	/** 获取登录用户的角色ID */
	public static String getGroupKeys(HttpSession session) {
		return (String) session.getAttribute(LOGIN_GROUP_KEY);
	}

	public static void clearLoginInfo(Session session) {
		session.removeAttribute(LOGIN_USER_INFO);
		session.removeAttribute(LOGIN_GROUP_KEY);
		session.removeAttribute(LOGIN_ROLE_KEY);
		session.removeAttribute(LOGIN_AVATAR);
		session.removeAttribute(LOGIN_NAME);
		session.removeAttribute(LOGIN_UUID);
	}

	public static void clearLoginInfo(HttpSession session) {
		session.removeAttribute(LOGIN_USER_INFO);
		session.removeAttribute(LOGIN_GROUP_KEY);
		session.removeAttribute(LOGIN_ROLE_KEY);
		session.removeAttribute(LOGIN_AVATAR);
		session.removeAttribute(LOGIN_NAME);
		session.removeAttribute(LOGIN_UUID);
	}

	public static void saveLoginInfo2(Session session, Collection<String> groups) {
		session.setAttribute(LOGIN_GROUP_KEY, CollUtil.join(groups, ","));
	}

	public static void saveLoginInfo1(Session session, Collection<String> roles) {
		session.setAttribute(LOGIN_ROLE_KEY, CollUtil.join(roles, ","));
	}

	public static void saveLoginInfo0(Session session, SysUser sysUser) {
		session.setAttribute(LOGIN_AVATAR, sysUser.getAvatar());
		session.setAttribute(LOGIN_NAME, sysUser.getName());
		session.setAttribute(LOGIN_UUID, sysUser.getUuid());
		session.setAttribute(LOGIN_USER_INFO, sysUser);
	}
}
